package com.nai365.nart.common.base.controller;

import com.dingxianginc.ctu.client.CaptchaClient;
import com.dingxianginc.ctu.client.model.CaptchaResponse;
import com.nai365.nart.common.base.vo.SmsVO;
import com.nai365.nart.common.core.util.R;
import com.nai365.nart.common.security.annotation.Inner;
import io.springboot.sms.core.SmsClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author baogs
 */
@RestController
@RequestMapping("/sms")
@Api(value = "sms", tags = "验证码模块")
public class SmsController {

	@Autowired
	private SmsClient smsClient;

	@PostMapping("/auth_send")
	@Inner(value = false)
	@ApiOperation(value = "发送手机短信验证码,带无感验证")
	public R sendMessageAuth(@RequestBody SmsVO smsVO) {
		String appId = "2ddf18a36857b10cae2dc66826fa1335";
		String appSecret = "89bcc921db0f8ff0a36ef05f21e8bf2c";
		CaptchaClient captchaClient = new CaptchaClient(appId, appSecret);
		captchaClient.setCaptchaUrl("https://api-vip.dingxiang-inc.com/api/tokenVerify");
		CaptchaResponse response = null;
		try {
			response = captchaClient.verifyToken(smsVO.getCaptchaToken());
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
		if (response.getResult()) {
			try {
				//短信接口相关信息返回给用户
				return R.ok(smsClient.sendCodeByKey("SMS_243180631", smsVO.getCode(), smsVO.getPhoneNumber()));
			} catch (Exception e) {
				return R.failed(e.getMessage());
			}
		} else {
			return R.failed("无感验证失败！");
		}
	}

	@PostMapping("/send")
	@Inner(value = false)
	public R sendMessage1(@RequestBody String number , String code){
		try {
			return R.ok(smsClient.sendCodeByKey("SMS_243180631",code,number));
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}
}
